package com.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.File;
import java.util.List;

import com.bean.Enterprise;
import com.service.EnterpriseService;
import com.service.impl.EnterpriseServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class EnterpriseModifyPhoto extends HttpServlet {

    private EnterpriseService enterpriseService=new EnterpriseServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("过来了！");
        Enterprise enterprise= (Enterprise) req.getSession().getAttribute("enterprise");
        String enterpriseid=enterprise.getEnterpriseid();

        //1、判断上传的数据是否多段数据
        if(ServletFileUpload.isMultipartContent(req)){
            //创建 FileItemFactory 工厂实现类
            FileItemFactory fileItemFactory=new DiskFileItemFactory();
            //创建用于解析上传数据的工具类 ServletFileUpload
            ServletFileUpload servletFileUpload=new ServletFileUpload(fileItemFactory);
            //解析上传数据，得到每一个表单项fileItem
            try {
                List<FileItem> list=servletFileUpload.parseRequest(req);
                String head_sculpture="";
                //循环判断，每一和表单项，是普通表单项，还是上传的文件
                for(FileItem fileItem:list){
                    if(fileItem.isFormField()){
                        //普通表单项
                        System.out.println("name属性值"+fileItem.getFieldName());
                        enterpriseid=fileItem.getString();
                        System.out.println("value属性值"+fileItem.getString("UTF-8"));
                    }else{
                        //上传的文件
                        System.out.println("name属性值"+fileItem.getFieldName());
                       /* head_sculpture=fileItem.getName();*/
                        head_sculpture=enterpriseid+".jpg";
                        System.out.println("文件名"+ head_sculpture);
                        System.out.println("文件名"+fileItem.getName());
                        String imgpath="D:\\IDEAProgram\\onlineflower\\web\\static\\picEnterprise\\"+head_sculpture;
                        File file=new File(imgpath);
                        if(file.exists()){
                            file.delete();
                            System.out.println(head_sculpture+"原来头像已经删除！");
                        }
                        fileItem.write(new File("D:\\IDEAProgram\\onlineflower\\web\\static\\picEnterprise\\"+ head_sculpture));
                        /*fileItem.write(new File("D:\\IDEAProgram\\FlowerStore\\web\\static\\picUser\\"+fileItem.getName()));*/
                    }
                }
                enterpriseService.modifyPhoto(enterpriseid,head_sculpture);
                enterprise.setHead_sculpture("head_sculpture");
                enterprise=enterpriseService.showMess(enterpriseid);
                req.getSession().setAttribute("enterprise",enterprise);
                req.getRequestDispatcher("/page/enterprise/showMess.jsp").forward(req,resp);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      doPost(req,resp);
    }
}
